import apis from '../../http/apis/apis';
export default {
    namespaced: true,

    state: {

        /*****************练超**************/
        adminsData: [],//普通管理员数据
        upAdminsData: {},//要修改的管理员数据
        ruleForm: {},//新增普通管理员数据
        seekAdminData: { status: "", value: "", type: "", total: 0, pageSize: 4, currentPage: 1 },//要查找管理员的数据
        adminDataSataus:0,
        /*****************练超*******************/

        /*****************************黄涛*****************/

        //出租方
        lessors: {
            type: "account", //收索类型
            value: "", //收索类型
            currentPage: 1, //当前页数
            pageSize: 3, //设置每页的数据条数
        },
        //租客
        tenants: {
            type: "account",
            value: "",
            currentPage: 1,
            pageSize: 3,
        }, //保存了租客的请求的基本信息

        //车位
        parkings: {
            type: "address",
            value: "",
            currentPage: 1,
            pageSize: 3,
        },

        lessorsData: { //保存返回的出租方的信息
            totalitem: 0,
            pagecount: 0,
            pagerows: []
        },
        tenantsData: { //保存返回的租客信息
            totalitem: 0,
            pagecount: 0,
            pagerows: []
        },
        parkingsData: { //保存后端返回的车位信息
            totalitem: 0,
            pagecount: 0,
            pagerows: []
        },

        pagerows: []//该数据用于保存出租方的的数据，便于排序
        /*****************************黄涛******************/
    },
    getters: {},
    mutations: {

        /*****************练草********** */
        changepagerows(state, val) {
            state.pagerows = val
        },
        changeAdminsData(state, getAdminsData) {
            state.adminsData = getAdminsData;
        },
        changeRuleForm(state, newRuleForm) {
            state.ruleForm = newRuleForm;
        },
        changeUpAdminsData(state, modifyAdminsId) {
            state.upAdminsData = state.adminsData.filter(item => item._id == modifyAdminsId._id)[0];
        }, 
        changeSeekAdminData(state, newSeekAdminData) {
            state.seekAdminData = { ...state.seekAdminData, ...newSeekAdminData };
        },
        //改变管理员状态
        changeAdminDataSataus(state,newAdminDataSataus){
            state.adminDataSataus = newAdminDataSataus;
        },
        //改变当前页
        changeCurrentPageLc(state, val) {
            state.seekAdminData.currentPage = val;
        },
        //改变每页显示条数
        changePageSizeLc(state, val) {
            state.seekAdminData.pageSize = val;
        },
        //改变总条数
        changeTotalLc(state, val) {
            state.seekAdminData.total = val;
        },
        /*****************练草****************/



        /*****************黄涛*****************/

        //保存从数据库中获取的值
        changelessorsData(state, value) {
            state.lessorsData = value;
            state.pagerows = value.pagerows
        },
        changetenantsData(state, value) {
            state.tenantsData = value;
        },
        changeparkingsData(state, value) {
            state.parkingsData = value;
        },
        //修改请求数据currtpage
        changeCurrentPagel(state, value) {

            state.lessors.currentPage = value;
        },
        changeCurrentPaget(state, value) {
            state.tenants.currentPage = value;
        },
        changeCurrentPagep(state, value) {
            state.parkings.currentPage = value;
        },
        //修改请求的value值
        changeValuel(state, value) {
            state.lessors.value = value;
        },
        changeValuet(state, value) {
            state.tenants.value = value;
        },
        changeValuep(state, value) {
            state.parkings.value = value;
        },
        //修改请求数据pagesize
        changePageSizel(state, value) {
            state.lessors.pageSize = value;
        },
        changePageSizet(state, value) {
            state.tenants.pageSize = value;
        },
        changePageSizep(state, value) {
            state.parkings.pageSize = value;
        },
        /**************黄涛*****************/

    },
    actions: {

        /*****************练超开始********** */
        //获取管理员数据
        async getAdmins(context,_id) {
            const { data } = await apis.admins.getAdmins(_id);
            context.commit('changeAdminsData', data);
            context.commit("changeAdminDataSataus",data[0].power);
        },

        //删除管理员
        async deleteAdmins(context, _id) {
            const { data } = await apis.admins.deleteAdmins({ _id });
            if (data.status) {
                alert("删除成功");
            }
        },

        //禁用或启用管理员
        async forbidAdmins(context, forbidAdminsData) {
            const { data } = await apis.admins.forbidAdmins(forbidAdminsData);
        },

        //新增普通管理员
        async addAdmins(context, newRuleForm) {
            context.commit('changeRuleForm', newRuleForm);
            const { data } = await apis.admins.addAdmins(context.state.ruleForm);
            if (data.status) {
                alert("新增管理成功");
            }
        },

        //修改管理员
        async modifyAdmins(context) {
            const { data } = await apis.admins.modifyAdmins(context.state.upAdminsData);
            if (data.status) {
                alert('修改成功');
            }
        },
        /*****************练草********** */
        //验证新增管理员账号，是否存在
        async isAdmins(context, newRuleForm) {
            context.commit('changeRuleForm', newRuleForm);
            const { data } = await apis.admins.isAdmins({
                account: context.state.ruleForm.account,
            });
            if (!data.status) {
                alert('管理员账号已存在，请重新输入');
            }
        },

        //查找管理员
        async seekAdmins(context) {
            let { status, value, type, pageSize, currentPage } = context.state.seekAdminData;
            if (status == '已禁用') {
                status = 2
            } else if (status == '未禁用') {
                status = 1
            }
            const { data } = await apis.admins.seekAdmins({ status, value, type, pageSize, currentPage });
            context.commit('changeAdminsData', data.data);//将查询到的数据，重新赋值给adminsData
            context.commit('changeTotalLc', data.total);//将查询到的总条数赋值给SeekAdminData的total
        },
        /*****************练超结尾********** */



        /*****************************黄涛**************** */
        /***************************出租方*****************/
        async getLessors(context) {
            const {
                data
            } = await apis.admins.getLessors(context.state.lessors);
            context.commit('changelessorsData', data.data);
            return data.data;
        },

        async modifyLessors(context, id) { //单独的更新state状态
            const { data } = await apis.admins.modifyLessors(id);
            return data;
        },

        async EditLessors(context, datas) {
            const { data } = await apis.admins.EditLessors(datas);
            if (data.status) {
                alert("修改成功");
            }
            else {
                alert("修改失败");
            }
            return data;
        },


        async deleteLessors(context, val) {
            const { data } = await apis.admins.deleteLessors(val);
            return data;
        },
        /*********停车位******* */

        async getParkings(context) {
            const {
                data
            } = await apis.admins.getParkings(context.state.parkings);
            context.commit('changeparkingsData', data.data);
            // console.log(data.data,"获取车位全部信息");
            return data.data;
        },

        async modifyParkings(context, id) { //单独的更新state状态

            const {
                data
            } = await apis.admins.modifyParkings(
                id
            );
            return data;
        },



        async EditParkings(context, datas) {
            const { data } = await apis.admins.EditParkings(datas);
            if (data.status) {
                alert("成功")
            }
            else {
                alert("修改失败");
            }
            return data;

        },


        async deleteParkings(context, val) {
            const {
                data
            } = await apis.admins.deleteParkings(val);
            return data;
        },

        /*********租客******* */
        async getTenants(context) {
            const {
                data
            } = await apis.admins.getTenants(context.state.tenants);
            context.commit('changetenantsData', data.data);
            return data.data;
        },
        //更改租客的认证状态
        async modifyTenants(context, val) {
            const {
                data
            } = await apis.admins.modifyTenants(val);
            return data;
        },
        async EditTenants(context, datas) {
            const { data } = await apis.admins.EditTenants(datas);
            if (data.status) {
                alert("修改成功");
            }
            else {
                alert("修改失败")
            }
            return data;
        },
        async deleteTenants(context, val) {
            const { data } = await apis.admins.deleteTenants(val);
            return data;
        },
    },
    /*****************************黄涛**************** */
}
